﻿using System.ComponentModel;

namespace CannedHeater.Model
{
    /// <summary>
    /// Represents settings for the Canned Heater program.
    /// Using singleton pattern
    /// </summary>
    public class Settings : INotifyPropertyChanged
    {
        private Settings() // Constructor should not be called from outside, use GlobalSettings property
        {

        }

        private static Settings _settingsInstance = null;
        /// <summary>
        /// Represents the single instance of Settings
        /// </summary>
        public static Settings GlobalSettings
        {
            get
            {
                if (_settingsInstance == null)
                {
                    _settingsInstance = new Settings();
                }
                return _settingsInstance;
            }
        } 

        /// <summary>
        /// Load a setting
        /// </summary>
        /// <param name="setting">The setting to load</param>
        /// <param name="val">The value to set the setting, null will have no effect</param>
        public void SetSetting(string setting, object val)
        {
            if (val == null) return; // Null is interpreted as "shouldn't change value"
            switch(setting)
            {
                case "NETDUINO_DEVICE_IP_AUTO":
                    NETDUINO_DEVICE_IP_AUTO = (bool)val;
                    break;
                case "NETDUINO_DEVICE_IP":
                    NETDUINO_DEVICE_IP = (string)val;
                    break;
                case "NETDUINO_DEVICE_PORT":
                    NETDUINO_DEVICE_PORT = (int)val;
                    break;
                case "PACHUBE_API_KEY":
                    PACHUBE_API_KEY = (string)val;
                    break;
                case "PACHUBE_FEED_ID":
                    PACHUBE_FEED_ID = (int)val;
                    break;
            }
        }

        private string _PACHUBE_API_KEY = ""; // Default
        /// <summary>
        /// The Pachube API Key
        /// </summary>
        public string PACHUBE_API_KEY
        {
            get { return _PACHUBE_API_KEY; }
            set
            {
                _PACHUBE_API_KEY = value;
                RaisePropertyChanged("PACHUBE_API_KEY");
            }
        }

        private int _NETDUINO_DEVICE_PORT = 35474; // Default
        /// <summary>
        /// The port to use for communication with netduino device
        /// </summary>
        public int NETDUINO_DEVICE_PORT
        {
            get { return _NETDUINO_DEVICE_PORT;  }
            set
            {
                _NETDUINO_DEVICE_PORT = value;
                RaisePropertyChanged("NETDUINO_DEVICE_PORT");
            }
        }

        private string _NETDUINO_DEVICE_IP = "127.0.0.1"; // Default
        /// <summary>
        /// The IP to use for communication with the netduino device
        /// </summary>
        public string NETDUINO_DEVICE_IP
        {
            get { return _NETDUINO_DEVICE_IP; }
            set
            {
                _NETDUINO_DEVICE_IP = value;
                RaisePropertyChanged("NETDUINO_DEVICE_IP");
            }

        }

        private bool _NETDUINO_DEVICE_IP_AUTO = false; // Default
        /// <summary>
        /// Whether or not the IP for the netdunio device should be fetched automatically
        /// </summary>
        // TODO: Not used yet
        public bool NETDUINO_DEVICE_IP_AUTO
        {
            get { return _NETDUINO_DEVICE_IP_AUTO;  }
            set
            {
                _NETDUINO_DEVICE_IP_AUTO = value;
                RaisePropertyChanged("NETDUINO_DEVICE_IP_AUTO");
            }
        }

        private int _PACHUBE_FEED_ID = 28679; // Default
        //private int _PACHUBE_FEED_ID = 35670;
        /// <summary>
        /// The Pachube Feed ID to fetch data from
        /// </summary>
        public int PACHUBE_FEED_ID
        {
            get { return _PACHUBE_FEED_ID;  }
            set
            {
                _PACHUBE_FEED_ID = value;
                RaisePropertyChanged("PACHUBE_FEED_ID");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }
}
